﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DefLib;

namespace GameUI
{
    public class UIunit
    {
        public List<Sprite> l_sprite;
        public float _x;
        public float _y;
        public int _w;
        public int _h;
        public List<int> sp_index;
        public List<int> sp_state;

        public UIunit()
        {
            _x = 0;
            _y = 0;
           // l_sprite = new List<Sprite>();
            sp_index = new List<int>();
            sp_state = new List<int>();
        }
        public virtual UIunit Clone(float x, float y, int w, int h)
        {
            UIunit uiu_clone = new UIunit();
            List<Sprite> l_sp = new List<Sprite>();
            for (int i_sp = 0; i_sp < l_sprite.Count; i_sp++)
            {
                l_sp.Add(l_sprite[i_sp].Clone(x, y, w, h));
            }

            uiu_clone.create(l_sp,
                x > 0 ? x : _x,
                y > 0 ? y : _y,
                w > 0 ? w : _w,
                h > 0 ? h : _h);

            return uiu_clone;
        }
        public virtual void create(List<Sprite> lS_interface, float x, float y, int w, int h)
        {
            l_sprite = lS_interface;
            _x = x;
            _y = y;
            _w = w;
            _h = h;
            
        }
        public virtual void update(float x,float y,int w,int h)
        {
            if (w * h != 0)
            {
                _x = x;
                _y = y;
                _w = w;
                _h = h;
            }

            for (int i = 0; i < sp_index.Count; i++)
            {
                if (sp_index[i] < l_sprite.Count)
                {
                    l_sprite[sp_index[i]].Update(sp_state[i], _x, _y, _w, _h);
                }
            }
        }
        public virtual void draw(SpriteBatch spriteBatch)
        {
            for(int i =0;i<sp_index.Count;i++)
            {
                if (sp_index[i] < l_sprite.Count)
                {
                    l_sprite[sp_index[i]].Draw(spriteBatch);
                }
            }
        }

        // Event
        public virtual bool is_choise()
        {
            if (Event.i_choise==0)
                return false;

            if (Event.chois_x < _x)
                return false;
            if (Event.chois_x >= _x + _w)
                return false;
            if (Event.chois_y < _y)
                return false;
            if (Event.chois_y >= _y + _h)
                return false;

            return true;
            
        }
        public virtual bool is_hover()
        {
            if (Event.i_choise == 1)
                return false;

            if (Event.chois_x < _x)
                return false;
            if (Event.chois_x >= _x + _w)
                return false;
            if (Event.chois_y < _y)
                return false;
            if (Event.chois_y >= _y + _h)
                return false;

            return true;
        }
    }
}
